package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/member")
public class LoginController {

    @Reference
    private MemberService memberService;
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/login")
    public Result checkUser(HttpServletRequest request,HttpServletResponse response, @RequestBody Map map){
        String validateCode = (String) map.get("validateCode");
        String telephone = (String) map.get("telephone");
        String code = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_LOGIN);
        if (code!=null&&validateCode!=null&&code.equals(validateCode)){
            Member member = memberService.findByTelephone(telephone);
            //验证码正确  判断是否会员
            if (member==null){
                member.setRegTime(new Date());
                member.setPhoneNumber(telephone);
                 memberService.add(member);
            }
            //向客户端浏览器写入Cookie;内容为手机号  追踪用户
            Cookie cookie = new Cookie("login_member_telephone", telephone);
            cookie.setPath("/");
            cookie.setMaxAge(60*60*24*30);
            response.addCookie(cookie);
            //将会员信息保存到redis中  redis无法直接存储对象 需要先转json字符串
            String s = JSON.toJSON(member).toString();
            jedisPool.getResource().setex(telephone,60*30,s);
            return  new Result(true,MessageConstant.LOGIN_SUCCESS);

        }else {
            return  new Result(false, MessageConstant.VALIDATECODE_ERROR);

        }
    }


    @RequestMapping("/findAll")
    public Result findAll(HttpServletRequest request,HttpServletResponse response){
       //获得保存在cook里的电话
        String telephone=null;
        Cookie[] cookies = request.getCookies();
        for (Cookie c : cookies) {
            String name = c.getName();
            if (name.equals("login_member_telephone")){
                telephone = c.getValue();
                System.out.println(telephone);
            }
        }

            try {
                   Member member =memberService.findByTelephone(telephone);

                return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS,member);
            } catch (Exception e) {
                return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
            }
    }


    @RequestMapping("/submit")
    public Result submit(@RequestBody Member member,String validateCode){
        String telephone =member.getPhoneNumber();
//        System.out.println(validateCode);
//        String validateCode = (String) map.get("validateCode");
        String code = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_LOGIN);
        if (validateCode == null || code == null || !validateCode.equals(code)) {
            //说明验证码不正确
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }

        try {
            memberService.edit(member);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS);
    }


}
